package jse;

import java.io.IOException;
import java.util.HashMap;

import javax.script.ScriptException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.io.ClassPathResource;

import jdk.nashorn.api.scripting.NashornScriptEngine;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import jse.util.JseMap;
import jse.util.ReaderUTF8;

public class JsRoute implements Route {

	@Override
	public Object run(JseMap map) {
		ClassPathResource res=new ClassPathResource(map.get("action")+".js");
		if(res.exists()) {//文件存在
//			ScriptEngineManager factory = new ScriptEngineManager();
	        //创建一个Nashorn脚本引擎
			NashornScriptEngineFactory nsef = new NashornScriptEngineFactory();
			NashornScriptEngine nse=(NashornScriptEngine) nsef.getScriptEngine("--language=es6","--optimistic-types=[true]");
			try {
				nse.eval(new ReaderUTF8(res.getInputStream()));
				Object re=nse.invokeFunction(map.get("func").toString(), map);
				return re;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

}
